<?php
namespace Ftt;

/**
 * Created by PhpStorm.
 * User: yy
 * Date: 16-10-27
 * Time: 下午9:47
 */
class View
{
    //public $viewPath =

    public $viewFileExt = '.phtml';

    /**
     * @param string $viewName
     * @param array $data
     * @throws Exception
     */
    public function render($viewName, array $data = [])
    {

        if (!is_dir(Ftt::$app->config['appPath'])) {
            throw new Exception('Config appPath is not exists!');
        }

        $viewFile = Ftt::$app->config['appPath'] . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $viewName . $this->viewFileExt;

        if (!file_exists($viewFile)) {
            throw new Exception("The view $viewFile is not exists!");
        }

        extract($data);

        require $viewFile;
    }

    /**
     * @param string $staticFile
     * @return string
     */
    public function assets($staticFile)
    {
        return dirname(parse_url($_SERVER['SCRIPT_NAME'], PHP_URL_PATH)) . '/' . $staticFile;
    }
}